package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.entity.OrderDetail;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.service.OrderDetailService;
import com.itheima.reggie.service.OrdersService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/order")
@Slf4j
public class OrdersController {

    @Autowired
    private OrdersService orderService;

    @Autowired
    private OrderDetailService orderDetailService;


    @PostMapping("/again")
    public R again(@RequestBody Orders order){
        log.info("订单order为 {}",order);
        orderService.again(order);
//        orderService.updateDishSort(orderDetails);
//        orderService.updateSetmealCode(orderDetails);
        return R.success("下单成功");
    }

    @GetMapping("/userPage")
    public R userPage(int page, int pageSize, HttpSession session) {
        log.info("page {},pageSize {}", page, pageSize);
        Page<Orders> ordersPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Orders> lqw = new LambdaQueryWrapper<>();
        lqw.orderByDesc(Orders::getCheckoutTime);
        Long userId = (Long) session.getAttribute("user");
        lqw.eq(Orders::getUserId, userId);
        orderService.page(ordersPage, lqw);

        Page<OrdersDto> ordersDtoPage = new Page<>();
        BeanUtils.copyProperties(ordersPage, ordersDtoPage, "records");

        List<Orders> ordersPageRecords = ordersPage.getRecords();

        List<OrdersDto> ordersDtos = ordersPageRecords.stream().map(item -> {
            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(item, ordersDto);
            return ordersDto;
        }).collect(Collectors.toList());

        if (ordersPageRecords.size() > 0) {
            List<OrdersDto> collect = ordersDtos.stream().map(item -> {
                LambdaQueryWrapper<OrderDetail> orderDetailLqw = new LambdaQueryWrapper<>();
                orderDetailLqw.eq(OrderDetail::getOrderId, item.getId());
                List<OrderDetail> orderDetails = orderDetailService.list(orderDetailLqw);
                item.setOrderDetails(orderDetails);
                return item;
            }).collect(Collectors.toList());
            ordersDtoPage.setRecords(collect);
        }
        return R.success(ordersDtoPage);
    }

    @PostMapping("/submit")
    public R submit(@RequestBody Orders orders) {
        List<OrderDetail> orderDetails = orderService.submit(orders);
        orderService.updateDishSort(orderDetails);
        orderService.updateSetmealCode(orderDetails);
        return R.success("下单成功");
    }

    @PutMapping
    public R update(@RequestBody Orders orders) {
        Orders order = orderService.getById(orders.getId());
        order.setStatus(orders.getStatus());
        LambdaQueryWrapper<Orders> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Orders::getId, order.getId());
        orderService.update(order, lqw);
        return R.success("修改成功");
    }

    @GetMapping("/page")
    public R page(int page, int pageSize, String number, String beginTime, String endTime) {
        log.info("{}", beginTime);
        Page<Orders> orderPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Orders> lqw = new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotEmpty(number), Orders::getNumber, number);
        lqw.between(beginTime != null, Orders::getOrderTime, beginTime, endTime);
        lqw.orderByDesc(Orders::getCheckoutTime);
        orderService.page(orderPage, lqw);
        return R.success(orderPage);
    }
}
